const gulp = require('gulp'),
	less = require('gulp-less'),
	babel = require('gulp-babel'),
  gulpCopy = require('gulp-contrib-copy'),
  clean = require('gulp-rm');

const srcDir = './src',
	distDir = './build';
const Dir = {
	stylesDir: [srcDir + '/public/styles/**/*.less', distDir + '/public/styles/'],
  otherScripts: [srcDir + '/**/*.es', distDir],
  pubScripts: [srcDir + '/public/scripts/**/*.js', distDir + '/public/scripts/'],
  libDir: [srcDir + '/lib/*/dist/**/*', distDir + '/public/lib/'],
  imgsDir: [srcDir + '/public/imgs/**/*', distDir + '/public/imgs/'],
  viewsDir: [srcDir + '/views/**/*.html', distDir + '/views/'],
  cleanScripts: './build' + '/**/*.js',
  cleanDir: distDir + '/**/*.*'
}

gulp.task('clean', () => {
  gulp.src(Dir.cleanDir, {read: false})
    .pipe(clean({ async: true }));
});

gulp.task('imgs', () => {
  gulp.src(Dir.imgsDir[0])
    .pipe(gulpCopy())
    .pipe(gulp.dest(Dir.imgsDir[1]));
});

gulp.task('lib', () => {
  gulp.src(Dir.libDir[0])
    .pipe(gulpCopy())
    .pipe(gulp.dest(Dir.libDir[1]));
});

gulp.task('views', () => {
  gulp.src(Dir.viewsDir[0])
    .pipe(gulpCopy())
    .pipe(gulp.dest(Dir.viewsDir[1]));
});

// 编译less文件
gulp.task('styles', () => {
  gulp.src(Dir.stylesDir[0])
    .pipe(less())
    .pipe(gulp.dest(Dir.stylesDir[1]));
});

gulp.task('clean-scripts', () => {
  gulp.src(Dir.cleanScripts, {read: false})
    .pipe(clean({ async: false }));
});

gulp.task('otherScripts', () => {
  gulp.src(Dir.otherScripts[0])
    .pipe(babel({
      presets: ['es2015', "stage-0"]
    }))
    .pipe(gulp.dest(Dir.otherScripts[1]));
});

// public/scripts下的文件仍然使用.js文件后缀，避免编译后的文件因为没有后缀名而报错
gulp.task('pubScripts', () => {
  gulp.src(Dir.pubScripts[0])
    .pipe(babel({
      presets: ['es2015', "stage-0"]
    }))
    .pipe(gulp.dest(Dir.pubScripts[1]));
});

gulp.task('scripts', ['clean-scripts', 'otherScripts'/*, 'pubScripts'*/]);
// gulp.task('static', ['imgs', 'lib', 'views']);

gulp.task('default', ['clean',/* 'static', 'styles',*/ 'otherScripts'/*, 'pubScripts'*/]);